//
//  JMTabBarController.m
//  OC-Sina
//
//  Created by 陈高健 on 15/12/21.
//  Copyright © 2015年 陈高健. All rights reserved.
//

#import "JMTabBarController.h"
#import "JMTabBar.h"
#import "JMHomeViewController.h"
#import "JMNavigationController.h"

@interface JMTabBarController ()<JMTabBarDelegate>

@end

@implementation JMTabBarController

- (void)viewDidLoad{
    [super viewDidLoad];
    
    //初始化TabBar
    JMTabBar *tabBar = [[JMTabBar alloc]init];
    //设置tabBar的代理
    tabBar.delegate = self;

    //通过KVC来设置自定义的tabBar
    [self setValue:tabBar forKeyPath:@"tabBar"];
    
    //创建首页控制器
    //UITableViewController *homeController = [[UITableViewController alloc]init];
    //[self addChildViewController:homeController imageName:@"tabbar_home" title:@"首页"];
    
    JMHomeViewController *homeController =[[JMHomeViewController alloc]init];
    [self addChildViewController:homeController imageName:@"tabbar_home" title:@"首页"];
    
    //添加消息控制器
    UITableViewController *msgConroller = [[UITableViewController alloc]init];
    [self addChildViewController:msgConroller imageName:@"tabbar_message_center" title:@"消息"];
    //添加发现控制器
    UITableViewController *discoverController = [[UITableViewController alloc]init];
    [self addChildViewController:discoverController imageName:@"tabbar_discover" title:@"发现"];
    //添加我的页面控制器
    UITableViewController *profileController = [[UITableViewController alloc]init];
    [self addChildViewController:profileController imageName:@"tabbar_profile" title:@"我"];
}

//添加子控制器到tabbar根控制器上
- (void)addChildViewController:(UIViewController *)childController imageName:(NSString *)imageName title:(NSString *)title{
    
    //设置tabbar默认的背景图片--->指定代码渲染模式为以原样模式的方式显示出来
    childController.tabBarItem.image = [[UIImage imageNamed:imageName]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    //设置tabbar选中时背景的图片
    childController.tabBarItem.selectedImage = [[UIImage imageNamed:[NSString stringWithFormat:@"%@_selected",imageName]]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    //设置tabbar的标题
    //childController.tabBarItem.title = title;
    //childController.navigationItem.title = title;
    
    childController.title = title;
    
    //设置图片(如果图片和文字在一张图片上的情况)往下偏移
    //childController.tabBarItem.imageInsets = UIEdgeInsetsMake(5, 0, 5, 0);
    
    //设置title颜色为橘黄色
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[NSForegroundColorAttributeName] = [UIColor orangeColor];
    
    //指定选中下的titile的颜色
    [childController.tabBarItem setTitleTextAttributes:dict forState:UIControlStateSelected];
    
    //设置头部UINavigationController,将子控制器包裹
//    UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:childController];
    JMNavigationController *navController = [[JMNavigationController alloc]initWithRootViewController:childController];
    
    //添加页面到根控制器(将设置的Nav控制器设置为JMTabBarController的子控制器)
    [self addChildViewController:navController];
}

//实现代理方法,点击按钮的时候调用
- (void)tabBar:(JMTabBar *)tabBar didSelectPlusButton:(UIButton *)button{
    NSLog(@"点击了放大按钮");
    
}

@end
